package com.central.apps.controller;

import com.central.app.service.AppCommonService;
import com.central.app.service.HandlerService;
import com.central.apps.dao.PmDao;
import com.central.apps.dao.SrDao;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Api(tags = "测试controller")
@RequestMapping("/SrWorkorder")
public class SrWoController {
    @Autowired
    AppCommonService appCommonService;

    @Autowired private HandlerService handlerService;
    @Autowired
    com.central.apps.dao.PmDao PmDao;

    @PostMapping("/CreateSrWo")
    public String CreateSrWo(@RequestBody JSONObject sr) throws Exception {
        Map<String,Object> workorder = new HashMap<>();
        com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
        log.info("================sid"+sr.getString("sid"));
        String wonum = quryMaxWonum("维修工单","SR-");
        String status = sr.getString("status");
        if(!(status.equals("待生成工单")  || status.equals("挂起"))){
            json.put("code", "1");
            json.put("msg", "此状态不允许生成工单！");

            return json.toJSONString();
        }

        if ( 0 == (int)sr.get("Repeatj") ){
           // handlerService.updateBySql("UPDATE bl_serviceapplication SET Repeatj = 1 WHERE sid ='"+sr.getString("sid")+"'" );
                    //"in (SELECT sid FROM bl_workorder WHERE sid=bl_serviceapplication.sid)");

            /*json.put("code", "0");
            json.put("msg", "服务工单创建完成！");
            workorder.put("DESCRIPTION","服务工单");
            workorder.put("UDWOTYPE","服务工单");
            workorder.put("sid",sr.get("sid"));
            workorder.put("ASSETNUM",sr.get("deviceid"));
            workorder.put("sbms",sr.get("devicedescription"));
            workorder.put("status","草稿");
            workorder.put("WONUM",wonum);
            Object Zl = appCommonService.saveOrUpdate("ServiceOrderagreementApp",workorder);*/
            String sitecode = "";
            if(sr.getString("sitecode")==null || "null".equals(sr.getString("sitecode"))){
                sitecode = "''";
            }else{
                sitecode = "'"+sr.getString("sitecode")+"'";
            }
            String orgcode = "";
            if(sr.getString("orgcode")==null || "null".equals(sr.getString("orgcode"))){
                orgcode = "''";
            }else{
                orgcode = "'"+sr.getString("orgcode")+"'";
            }

            String deptcode = "";
            if(sr.getString("deptcode")==null || "null".equals(sr.getString("deptcode"))){
                deptcode = "''";
            }else{
                deptcode = "'"+sr.getString("deptcode")+"'";
            }

            //"+sr.get("reporttype")+",默认生成维修工单
            handlerService.updateBySql("insert into bl_workorder (wonum,description,createuser,updatetime,createtime,udwotype,status,sid,ASSETNUM,sbms,deptcode,sitecode,orgcode,test,faultlevel,FAILURECODE,failurecodedesc,PROBLEMCODE,problemcodedesc,shutdowntype)values('"+wonum+"','"+sr.get("sname")+"','"+sr.getString("createuser")+"',now(),now(),'维修工单','草稿','"+sr.get("sid")+"','"+sr.get("zcbh")+"','"+sr.get("zcms")+"',"+deptcode+","+sitecode+","+orgcode+",'1','"+sr.getString("faultlevel")+"','"+sr.getString("gztx")+"','"+sr.getString("txms")+"','"+sr.getString("gzxx")+"','"+sr.getString("xxms")+"','"+sr.getString("tjlx")+"')");

            handlerService.updateBySql("UPDATE bl_serviceapplication SET Repeatj = 1 ,status='已生成工单',inputGoto ='"+wonum+"'WHERE sid ='"+sr.getString("sid")+"'");
            json.put("code", "0");
            json.put("msg", "维修工单创建完成！");

        }else {
            json.put("code", "1");
            json.put("msg", "已生成工单，不允许再次生成！");
        }
        return json.toJSONString();


    }

    @PostMapping("/hangUp")
    public String hangUp(@RequestBody JSONObject sr) throws Exception {

        com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
        String status = sr.getString("status");
        if(status.equals("已关闭")){
            json.put("code", "1");
            json.put("msg", "已关闭,禁止挂起！");

            return json.toJSONString();
        }
        if(status.equals("已生成工单")){
            json.put("code", "1");
            json.put("msg", "已生成工单,禁止挂起！");

            return json.toJSONString();
        }


        if(!status.equals("待生成工单")){
            json.put("code", "1");
            json.put("msg", "此状态不允许挂起！");

            return json.toJSONString();
        }

        handlerService.updateBySql("UPDATE bl_serviceapplication SET status ='挂起' WHERE sid ='"+sr.getString("sid")+"'");
        json.put("code", "0");
        json.put("msg", "已挂起！");

        return json.toJSONString();


    }

    private String quryMaxWonum(String woType, String woFax) {
        String nowWonum = PmDao.qurySrMaxWonum(woType);
        if("".equals(nowWonum) || nowWonum==null){
            nowWonum =  "100000";
        }else{
            nowWonum = String.format("%06d",
                    new Object[] {
                            Integer.valueOf(Integer.parseInt(nowWonum.substring(nowWonum.length() - 6)) + 1) });
        }
        nowWonum = woFax + nowWonum;

        return nowWonum;
    }


    @PostMapping("/UpdateSr")
    public String UpdateSr(@RequestBody JSONObject sr) throws Exception {

        com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
        log.info("================sid"+sr.getString("sid"));


            handlerService.updateBySql("UPDATE bl_serviceapplication SET Repeatj = 1 ,inputGoto = (SELECT WONUM FROM bl_workorder WHERE sid ='"+sr.getString("sid")+"') WHERE sid ='"+sr.getString("sid")+"'");

        json.put("code", "2");
        json.put("msg", "信息更新成功！");
        return json.toJSONString();

    }


}
